/*---------------------------------------------------------------------------------------------
 *  Copyright (c) kylinIDETeam. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

// import { Action } from 'vs/base/common/actions';
// import { isWeb } from 'vs/base/common/platform';
import { localize } from 'vs/nls';
import { Emitter, Event } from 'vs/base/common/event';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { IExtensionsGalleryConfiguration } from 'vs/platform/extensionsGallery/common/extensionsGallery.config.contribution';
import { IExtensionsGalleryConfigService } from 'vs/platform/extensionsGallery/common/extensionsGalleryConfigService';
import { INotificationService } from 'vs/platform/notification/common/notification';
import { debounce } from 'vs/base/common/decorators';
// import { IHostService } from 'vs/workbench/services/host/browser/host';

export class ExtensionsGalleryConfigService implements IExtensionsGalleryConfigService {
	declare readonly _serviceBrand: undefined;
	private _serviceUrl: string = '';
	private _itemUrl: string = '';
	public extensionsGalleryConfiguration!: IExtensionsGalleryConfiguration;
	private preExtensionsGalleryConfig: IExtensionsGalleryConfiguration;

	private readonly _onConfigChange = new Emitter<string>();
	readonly onConfigChange: Event<string> = this._onConfigChange.event;

	constructor(
		@IConfigurationService private readonly configurationService: IConfigurationService,
		@INotificationService private readonly notificationService: INotificationService,
		// @IHostService private readonly hostService: IHostService,
	) {
		this.preExtensionsGalleryConfig = this.configurationService.getValue<IExtensionsGalleryConfiguration>('extensionsGallery');

		this.updateURL(this.preExtensionsGalleryConfig);
		this.configurationService.onDidChangeConfiguration(() => this.configure(this.configurationService.getValue<IExtensionsGalleryConfiguration>('extensionsGallery')), this);
	}

	get serviceUrl(): string {
		return this._serviceUrl;
	}
	get itemUrl(): string {
		return this._itemUrl;
	}
	private updateURL(extensionsGalleryConfig: IExtensionsGalleryConfiguration) {
		if (extensionsGalleryConfig) {
			this._serviceUrl = extensionsGalleryConfig.serviceUrl;
			this._itemUrl = extensionsGalleryConfig.itemUrl;
		}
	}

	private configure(extensionsGalleryConfig: IExtensionsGalleryConfiguration) {
		//不能直接使用==判断，可以将内容转为json的比较，必须序列一致
		if (JSON.stringify(this.preExtensionsGalleryConfig) === JSON.stringify(extensionsGalleryConfig)) {
			//前一次和本次修改一致，不做任何操作
		} else {
			this.preExtensionsGalleryConfig = extensionsGalleryConfig;
			this.updateURL(extensionsGalleryConfig);
			// if (extensionsGalleryConfig.serviceUrl) {
			// this._serviceUrl = extensionsGalleryConfig!.serviceUrl!;
			// this._itemUrl = extensionsGalleryConfig.itemUrl;
			this._onConfigChange.fire(this._serviceUrl ? this._serviceUrl : '');

			this.showMessage();
		}
	}

	//notification
	@debounce(2000)
	showMessage() {
		this.notificationService.info(localize('galleryConfigChanged', "Extensions Gallery Config Changed"));

		// this.notificationService.prompt(
		// 	Severity.Info,
		// 	"changed testing",
		// 	[
		// 		{
		// 			label: isWeb ? localize('reload', "Reload") : localize('restart', "Restart"),
		// 			run: () => this.hostService.restart()
		// 		}
		// 	]
		// );

		// this.notificationService.notify({
		// 	severity: Severity.Info,
		// 	message: "satest",
		// 	actions: {
		// 		primary: [
		// 			new Action('Restart', isWeb ? localize('reload', "Reload") : localize('restart', "Restart"), undefined, true, () => this.hostService.restart())
		// 		]
		// 	}
		// });
		// }
	}


}
